package com.auto.server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) {
		try {
			ServerSocket server = new ServerSocket(6969);
			//使用accept()阻塞等待客户请求，有客户就反应
			Socket socket =server.accept();
			String line;
			//由Socket对象得到输入流，并构造相应的BufferedReader对象
			BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			//PrintWriter os=new PrintWriter(socket.getOutputStream());
			//由Socket对象得到输出流，并构造PrintWriter对象
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());
			//BufferedReader sin=new BufferedReader(new FileReader("E:\\storm.txt"));
			BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
			//由系统标准输入设备构造BufferedReader对象
			System.out.println("Client:"+is.readLine());
			//准备发送给客户端的数据
			line=sin.readLine();
			//从标准输入读入一字符串
			while(!line.equals("bye")){
				//如果该字符串为 "bye"，则停止循环
				os.writeUTF(line);
					//向客户端输出该字符串
				os.flush();
				//刷新输出流，使Client马上收到该字符串
				System.out.println("Server:"+line);
				//在系统标准输出上打印读入的字符串
				System.out.println("Client:"+is.readLine());
				//从Client读入一字符串，并打印到标准输出上
				line=sin.readLine();
				//从系统标准输入读入一字符串
				} //继续循环
				os.close(); //关闭Socket输出流
				is.close(); //关闭Socket输入流
				socket.close(); //关闭Socket
				server.close(); //关闭ServerSocket
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
